<?php

//define( "ARROW_MODELS_PATH", ARROW_ABSOLUTE_PATH."/standardModels" );
define( "ARROW_CONF_PATH", ARROW_ABSOLUTE_PATH."/conf" );
define( "ARROW_LIBS_PATH", ARROW_ABSOLUTE_PATH."/libs" );
define( "ARROW_COMPONENTS_PATH", ARROW_ABSOLUTE_PATH."/components" );

require_once(ARROW_CORE_PATH."/ArrowObject.php");
require_once(ARROW_CORE_PATH."/ArrowCacheProvider.php");
require_once(ARROW_CORE_PATH."/interfaces/IArrowCacheable.php");

class ArrowServerInit {
  
  private static $file_name = "/models_cache/cache_server.php";
  private static $removeWhiteSpace = false;
  private static $core_requires = array( 
 	'/interfaces/IArrowSingleton.php',
	'/interfaces/IArrowSerializable.php',
	'/interfaces/IArrowModel.php',
	'/interfaces/IArrowIdentificable.php',
	'/interfaces/handlers/IAccessHandler.php',
	'/interfaces/handlers/IAuthHandler.php',
	'/interfaces/handlers/IErrorHandler.php',
	'/interfaces/handlers/IExceptionHandler.php',
	'/interfaces/handlers/IRemoteResponseHandler.php',
	'/interfaces/handlers/ISessionHandler.php',
	"/ArrowLogger.php",
	"/ArrowException.php",
	"/ArrowCoreException.php",
	"/ArrowConfProvider.php",
	"/ArrowErrorHandler.php",
	"/ArrowController.php",
	"/ArrowRequestContext.php",
	"/ArrowRouter.php",
	"/ArrowTemplateParser.php",
	"/ArrowView.php"
  ) ;
  
  private function __construct() {}
  
  public static function init() {
  error_reporting(E_ALL);	
	self::loadCoreFiles();
	foreach( self::$core_requires as $path ){
		//require_once( ARROW_CORE_PATH.$path );
	}
	
	$includedModels = ArrowCacheProvider::importModelsCache();
	ArrowController::setLoadedModels($includedModels);
		
	ArrowController::importModel( 'fb.Fb' );	
	//self::loadCoreFiles();
	
  }
  
  private static function loadCoreFiles() {
	//if( !file_exists( ARROW_CACHE_PATH. self::$file_name ) )  TODO: włączyc ten cache jak już będzie serwer ok
		 self::generateCache();
		 
	require_once ARROW_CACHE_PATH. self::$file_name ;	
  }
  
  private static function generateCache( ) {
  $code = "";
	foreach( self::$core_requires as $path ){
		$file = file_get_contents(ARROW_CORE_PATH.$path);
		//$file = preg_replace( "/ArrowController *?:: *?importModel.+?\;/", "", $file );
		$file = str_replace( array( "__FILE__", "<?php", "?>" ), array( "'".$path."'", "", "" ),$file);
		$code.="\n".$file;
	}

	if(function_exists("token_get_all")){
		$tokens = token_get_all($code);
		$code = "";
		foreach ($tokens as $token) {
		   if (is_string($token)) {
		       // simple 1-character token
		       $code.=$token;
		   } else {
		       // token array
		       list($id, $text) = $token;
				
		       switch ($id) { 
		           case T_COMMENT: 
		           case T_OPEN_TAG:
		           case T_CLOSE_TAG:
		           default:
		               $code.=$text;
	               break;
		       }
		   }
		} 
	}else{
		$code = preg_replace("/<\?php[\r ]{0,}\n/ms", "",$code);
		$code = preg_replace("/[\r ]{0,}\n[ ]{0,}\?>/ms", "",$code);
	}
	file_put_contents(ARROW_CACHE_PATH.self::$file_name, "<?php\n".$code."\n?>");
	if(self::$removeWhiteSpace)
		file_put_contents(ARROW_CACHE_PATH.self::$file_name, php_strip_whitespace(ARROW_CACHE_PATH.self::$file_name));
  }

}

?>